package com.guoqing.example.utils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.google.common.collect.Lists;
import com.guoqing.example.Req;
import com.guoqing.example.model.UserInfo;
import com.guoqing.example.model.UserInfo2;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.BeanWrapper;
import org.springframework.beans.BeanWrapperImpl;
import org.springframework.util.StringUtils;

import java.beans.PropertyDescriptor;
import java.util.*;

/**
 * @author guoQ.z
 * @date 2021/1/20 9:41
 * TODO
 */
@Slf4j
public class BeanConvert {

    public static void convertSkipNull(Object source, Object target) {
        BeanUtils.copyProperties(source, target, getNullProperty(source));
    }

    public static String[] getNullProperty(Object source) {
        BeanWrapper wrapper = new BeanWrapperImpl(source);
        PropertyDescriptor[] propertyDescriptors = wrapper.getPropertyDescriptors();
        Set<String> set = new HashSet<>();
        for (PropertyDescriptor property : propertyDescriptors) {
            Object value = wrapper.getPropertyValue(property.getName());
            if (Objects.isNull(value)) {
                set.add(property.getName());
            }
        }
        return StringUtils.toStringArray(set);
    }

    public static <T> T convert(Object sourceBean, Class<T> clazz) {
        try {
            T t = clazz.newInstance();
            BeanUtils.copyProperties(sourceBean, t);
            return t;
        } catch (Exception ex) {
            log.error("对象转换异常，message:{}", ex.getMessage());
            throw new RuntimeException("对象转换异常," + ex.getMessage());
        }
    }

    public static <T, R> List<R> convertList(List<T> sourceList, Class<R> clazz) {
        if (CollectionUtils.isEmpty(sourceList)) {
            return Lists.newArrayList();
        }
        String jsonString = JSON.toJSONString(sourceList);
        return JSON.parseArray(jsonString, clazz);
    }

    public static Map<String, String> convertMap(Object obj) {
        String jsonString = JSON.toJSONString(obj);
        return JSON.parseObject(jsonString, new TypeReference<Map<String, String>>() {
        });
    }

    public static <R> List<R> copy(List<?> sourceList, Class<R> clazz) {
        String s = JSON.toJSONString(sourceList);
        return JSON.parseArray(s, clazz);
    }


    public static void main(String[] args) {
        UserInfo info = new UserInfo();
        info.setName("123");
        info.setGender("M");

        UserInfo info2 = new UserInfo();
        info2.setName("info2");
        info2.setGender("F");

        List<UserInfo> list = new ArrayList<>();
        list.add(info);
        list.add(info2);


        UserInfo2 info201 = new UserInfo2();
        info201.setName2("123-201");
        info201.setGender2("M-201");

        UserInfo2 info202 = new UserInfo2();
        info202.setName2("info2-202");
        info202.setGender2("F-202");

        List<UserInfo2> list2 = new ArrayList<>();
        list2.add(info201);
        list2.add(info202);

        Req req = new Req();
        req.setList(list);
        req.setList2(list2);

        System.out.println(list);
        System.out.println(list2);

        List<UserInfo> copy = copy(req.getList(), UserInfo.class);
        List<UserInfo2> copy1 = copy(req.getList2(), UserInfo2.class);
        System.out.println(copy);
        System.out.println(copy1);
    }
}
